﻿using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	#region public
	/// <summary>
	/// X轴、Y轴移动速度
	/// </summary>
	public float xSpeed = 0.01f,ySpeed = 0.01f;
	/// <summary>
	/// 所受重力
	/// </summary>
	public float gravityScale = 1f;
	#endregion
	#region private
	/// <summary>
	/// 是否在移动
	/// </summary>
	private bool _isRunning; 
	/// <summary>
	/// 移动X轴、Y轴方向
	/// </summary>
	private int _xDirection,_yDirection;
	/// <summary>
	/// 是否可
	/// </summary>
	private bool _canClimb;

	private bool _isClimbing;

	private float _ladderBottomY;

	private Rigidbody2D _rigidbody2D;

	private Controller.ControllerManager _controllerManager;

	private Animator _animator;

	#endregion

	void Start(){
		GameObject obj = GameObject.FindWithTag("Controller");
		if(obj){
			_controllerManager = obj.GetComponent<Controller.ControllerManager>();
			if(_controllerManager){
				_controllerManager.SetArrowPressedEvent(MoveLeft,MoveRight,MoveUp,MoveDown);
			}
		}
		_rigidbody2D = GetComponent<Rigidbody2D>();
		_rigidbody2D.gravityScale = gravityScale;
		_animator = GetComponent<Animator>();
	}

	// Update is called once per frame
	void Update () {
		if(!GameSetting.GAME_START){
			return;
		}
		Move();
	}

	void Move(){
		if(Input.GetKey(KeyCode.A)){
			MoveLeft();
		}
		else if (Input.GetKey(KeyCode.D)){
			MoveRight();
		}
		else if(Input.GetKey(KeyCode.W)){
			MoveUp();
		}
		else if(Input.GetKey(KeyCode.S)){
			MoveDown();
		}

		if(_isRunning){
			Run();
		}
		if(_isClimbing){
			UpOrDown();
		}
	}
	void MoveLeft(){
		_isRunning = true;
		_xDirection = -1;
		if(transform.localRotation.y != 1){
			transform.localRotation = new Quaternion(0,1.0f,0,0);
		}
	}
	void MoveRight(){
		_isRunning = true;
		_xDirection = 1;
		if(transform.localRotation.y != 0){
			transform.localRotation = new Quaternion(0,0,0,1.0f);
		}
	}
	void MoveUp(){
		if(_canClimb){
			_isClimbing = true;
			_yDirection = 1;
		}
	}
	void MoveDown(){
		if(_canClimb){
			if( transform.position.y <= _ladderBottomY ){
				return;
			}
			_isClimbing = true;
			_yDirection = -1;
		}
	}
	/// <summary>
	/// Enter the lader.
	/// </summary>
	/// <param name="ladder">Ladder.</param>
	void EnterLader(Ladder ladder){
		_canClimb = true;
		_ladderBottomY = ladder.transform.position.y + ladder.bottomY;
		_rigidbody2D.gravityScale = 0;
		_rigidbody2D.isKinematic = true;
	}
	/// <summary>
	/// Exit the lader.
	/// </summary>
	/// <param name="ladder">Ladder.</param>
	void ExitLader(Ladder ladder){
		_canClimb = false;
		_ladderBottomY = ladder.transform.position.y + ladder.bottomY;
		_rigidbody2D.gravityScale = gravityScale;
		_rigidbody2D.isKinematic = false;
	}
	private void Run(){
		if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Run")){
			transform.position = new Vector3(transform.position.x + _xDirection * xSpeed,transform.position.y,transform.position.z);
			_isRunning = false;
		}
		if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")){
			_animator.SetTrigger("Run");
		}
	}
	private void UpOrDown(){
		if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Run")){
			transform.position = new Vector3(transform.position.x ,transform.position.y + ySpeed * _yDirection,transform.position.z);
			_isClimbing = false;
		}
		if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Idle")){
			_animator.SetTrigger("Run");
		}
	}
}
